package com.xmall.sys.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.sys.DictValueDto;
import com.xmall.sys.define.request.dict.DictValueUpdateRequest;
import com.xmall.sys.service.IDictValueService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统字典
 */
@RequestMapping("/service/sys/dict/value")
@RestController
public class DictValueController {

    @Resource
    private IDictValueService dictValueService;

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<DictValueDto> info(@PathVariable("id") Long id){
        return Result.success(dictValueService.info(id));
    }

    /**
     * 列表
     */
    @PreAuthorize("hasAuthority('sys:dict:list')")
    @GetMapping("/list/{dictId}")
    public Result<List<DictValueDto>> list(@PathVariable("dictId") Long dictId){
        List<DictValueDto> pageList = dictValueService.list(dictId);
        return Result.success(pageList);
    }

    /**
     * 保存
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('sys:dict:save')")
    @PostMapping("/save")
    public Result saveEntity(@Validated(value = AddGroup.class) @RequestBody DictValueUpdateRequest request){
        dictValueService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:dict:update')")
    @PostMapping("/update")
    public Result updateEntity(@Validated(value = UpdateGroup.class) @RequestBody DictValueUpdateRequest request){
        dictValueService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    @PostMapping("/delete/{id}")
    Result delete(@PathVariable("id") Long id){
        dictValueService.deleteEntity(id);
        return Result.success();
    }

}
